local danzhe = fk.CreateSkill{
    name = "ym1__danzhe",
}
Fk:loadTranslationTable{
    ["ym1__danzhe"] = "啖蔗",
    [":ym1__danzhe"] = "每回合每种牌名限一次，你可以将一张牌当牌面字数大于之的另一张单体即时牌使用。若为♠牌，你<a href = 'ym1__jinruyixiachupaijieduan'><font color='#b97eb4'>进入一下出牌阶段</font></a>。",
    ["ym1__jinruyixiachupaijieduan"] = "<b>进入一下出牌阶段</b>：<br>执行一次出牌阶段空闲时间点的结算。（即使用一张可使用的牌或发动一次出牌阶段空闲时间点可发动的技能）",
}
--- 获得卡牌的牌面字数长度
---@param card Card|integer
---@return integer
local function getCardWordNumber(card)
    if type(card) == "number" then card = Fk:getCardById(card) end
    Fk:filterCard(card.id)
    local num = 0
    local str = card.trueName
    -- 防止没有翻译真名
    if str == Fk:translate(str, "zh_CN") then
        str = card.name
    end
    num = num + Fk:translate(str, "zh_CN"):len()
    if card.trueName ~= "shade" then
        num = num + Fk:translate(":"..card.name, "zh_CN"):gsub("<[^>]+>", ""):len()
    else
        num = num + ("没有效果，不能被使用。当【影】进入弃牌堆后移出游戏。"):len()
    end
    return num
end
danzhe:addEffect("viewas",{
    pattern = ".|.|.|.|.|basic,trick",
    interaction = function(self, player)
        local all_choices = table.filter(Fk:getAllCardNames("bt"), function (name)
            local c = Fk:cloneCard(name)
            for _, cardID in ipairs(player:getCardIds("he")) do
                Fk:filterCard(cardID, player)
                if getCardWordNumber(cardID) < getCardWordNumber(c) and not c.multiple_targets then
                    return true
                end
            end
        end)
        local choices = player:getViewAsCardNames(danzhe.name, all_choices, nil, player:getTableMark("ym1__danzhe-turn"))
        if #choices > 0 then
            return UI.CardNameBox { choices = choices, all_choices = all_choices }
        end
    end,
    handly_pile = true,
    card_filter = function(self, player, to_select, selected)
        if self.interaction.data then
            local name = self.interaction.data
            local c = Fk:cloneCard(name)
            Fk:filterCard(to_select, player)
            return #selected == 0 and getCardWordNumber(to_select) < getCardWordNumber(c)
        else
            return false
        end
    end,
    view_as = function (self, player, cards)
        local name = self.interaction.data
        if not name or #cards ~= 1 then return nil end
        local c = Fk:cloneCard(name)
        c:addSubcards(cards)
        c.skillName = danzhe.name
        return c
    end,
    before_use = function(self, player, use)
        local room = player.room
        room:addTableMark(player, "ym1__danzhe-turn", use.card.trueName)
        if Fk:getCardById(use.card.subcards[1]).suit == Card.Spade then
            use.extra_data = use.extra_data or {}
            use.extra_data.ym1__danzhe = true
        end
    end,
    after_use = function (self, player, use)
        local room = player.room
        if use.extra_data and use.extra_data.ym1__danzhe then
            local data = { timeout = room.timeout }
            room.logic:trigger(fk.StartPlayCard, player, data, true)

            local req = Request:new(player, "PlayCard")
            req.timeout = data.timeout
            local result = req:getResult(player)

            if result ~= "" then
                local useResult = room:handleUseCardReply(player, result)
                if type(useResult) == "table" then
                    room:useCard(useResult)
                end
            end
        end
    end,
    enabled_at_play = function(self, player)
        return #player:getViewAsCardNames(danzhe.name, table.filter(Fk:getAllCardNames("bt"), function (name)
            local c = Fk:cloneCard(name)
            for _, cardID in ipairs(player:getCardIds("he")) do
                if getCardWordNumber(cardID) < getCardWordNumber(c) and not c.multiple_targets then
                    return true
                end
            end
        end), nil, player:getTableMark("ym1__danzhe-turn")) > 0
    end,
    enabled_at_response = function(self, player, response)
        return not response and #player:getViewAsCardNames(danzhe.name, table.filter(Fk:getAllCardNames("bt"), function (name)
            local c = Fk:cloneCard(name)
            for _, cardID in ipairs(player:getCardIds("he")) do
                if getCardWordNumber(cardID) < getCardWordNumber(c) and not c.multiple_targets then
                    return true
                end
            end
        end), nil, player:getTableMark("ym1__danzhe-turn")) > 0
    end,
})
return danzhe